package dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.Student;
import utils.DBUtils;
import utils.SqlUtils;

import java.sql.SQLException;
import java.util.List;

public class StudentDaoImpl implements StudentDao{

    private QueryRunner queryRunner=new QueryRunner(DBUtils.getSource());

    @Override
    public void addStudent(Student student) throws SQLException {
        String sql = SqlUtils.getSql("student.add");
        long result = queryRunner.insert(sql,new ScalarHandler<>(),student.getStuName(),student.getStuGender(),student.getStuPhone(),student.getBornDate(),student.getStuEmail(),student.getStuAddress(),student.getStuId());

    }

    @Override
    public List<Student> findStudentsByPage(int pyl, int pageSize) throws SQLException {
        String sql = SqlUtils.getSql("student.querylist");
        List<Student> studentList = queryRunner.query(sql,new BeanListHandler<>(Student.class),pyl,pageSize);
        return studentList;
    }

    @Override
    public long getCount() throws SQLException {
        String sql = SqlUtils.getSql("student.count");
        long count = queryRunner.query(sql, new ScalarHandler<>());
        return count;
    }

    @Override
    public void deleteById(long stuId) throws SQLException {
        String sql = SqlUtils.getSql("student.delete");
        queryRunner.update(sql,stuId);


    }
}
